package app.cn.com.school.adapter

import android.content.ContentValues
import android.content.Context
import android.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.CheckBox
import app.cn.com.school.R
import app.cn.com.school.databinding.ShopcatGoodsItemBinding
import app.cn.com.school.databinding.ShopcatGroupItemBinding

/**
 * Created by Administrator on 2017/11/3 0003.
 */
class ShopCatAdapter(private val context: Context, private val contentValues: List<ContentValues>, private val cvs: List<List<ContentValues>>, private val onClick: CheckInterface) : BaseExpandableListAdapter() {
    override fun getGroup(groupPosition: Int): Any {
        return contentValues[groupPosition]
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return false
    }

    override fun hasStableIds(): Boolean {
        return false
    }

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        var bindingGroup: ShopcatGroupItemBinding
        var convertView = convertView
        if (convertView == null) {
            bindingGroup = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.shopcat_group_item, parent, false)
            convertView = bindingGroup.root
            convertView!!.tag = bindingGroup
        } else {
            bindingGroup = convertView!!.tag as ShopcatGroupItemBinding
        }
        bindingGroup.storeName.text = contentValues[groupPosition].getAsString("ddd")
        bindingGroup.storeCheckBox.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                onClick.checkGroup(groupPosition, (v as CheckBox).isChecked)
            }
        })

        if (contentValues!!.size <= groupPosition || contentValues == null) {
            return convertView
        }

        return convertView
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return contentValues[groupPosition].getAsInteger("")
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return cvs[groupPosition][childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        var binding: ShopcatGoodsItemBinding
        var convertView = convertView
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.shopcat_goods_item, parent, false)
            convertView = binding.root
            convertView!!.tag = binding
        } else {
            binding = convertView!!.tag as ShopcatGoodsItemBinding
        }
        if (cvs[groupPosition]!!.size <= childPosition || contentValues == null) {
            return convertView
        }

        return convertView
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun getGroupCount(): Int {
        return contentValues.size
    }


    /**
     * 店铺的复选框
     */
    interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素的位置
         * @param isChecked     组元素的选中与否
         */
        fun checkGroup(groupPosition: Int, isChecked: Boolean)

        /**
         * 子选框状态改变触发的事件
         *
         * @param groupPosition 组元素的位置
         * @param childPosition 子元素的位置
         * @param isChecked     子元素的选中与否
         */
        fun checkChild(groupPosition: Int, childPosition: Int, isChecked: Boolean)
    }
    /////e:ee:taobao-master   --购物车的模型
}